﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;

namespace School.Pages.Teaching
{
    public partial class TE_Topic_HomeWork : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                FillControls();
                ddlBind(ddlCat, School.Teaching.Teaching.Get_Question_Categories());
            }
            lbltst.Visible = false;

        }
        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));
            }
        }

        protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsubject.SelectedValue != "0")
            {
                ddlBind(ddlCourse, School.Teaching.Teaching.Get_Course_By_Subject(Convert.ToInt32(ddlsubject.SelectedValue)));
            }
        }
        protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCourse.SelectedIndex > 0)
            {
                ddlBind(ddlTopic, School.Teaching.Teaching.Get_Topic_ByCourse(Convert.ToInt32(ddlCourse.SelectedValue)));
            }
        }
        protected void ddlCat_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCat.SelectedIndex > 0)
            {
                gvBank_QBind();
            }
        }
        private void gvBank_QBind()
        {
            gvBank_Q.DataSource = School.Teaching.Teaching.Get_Bank_Question_Homwork_BySubj(Convert.ToInt32(ddlsubject.SelectedValue), Convert.ToInt32(ddlCat.SelectedValue));
            gvBank_Q.DataBind();
        }
        //Get_Bank_Question_Homwork_BySubj
        protected void gvBank_Q_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBank_Q.PageIndex = e.NewPageIndex;
            gvBank_QBind();
        }

        protected void ddlTopic_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlTopic.SelectedIndex > 0)
            {
                gvBank_QinBind();
            }
        }

        private void gvBank_QinBind()
        {
            gvBank_Qin.DataSource = School.Teaching.Teaching.Get_Topic_Homwork_ByCat(Convert.ToInt32(ddlTopic.SelectedValue));
            gvBank_Qin.DataBind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            bool re = false;

            foreach (GridViewRow Row in gvBank_Q.Rows)
            {
                CheckBox ch = (CheckBox)Row.FindControl("Chchoice");

                if (ch.Checked)
                {
                    int Id = Convert.ToInt32(gvBank_Q.DataKeys[Row.RowIndex].Value.ToString());
                    TextBox txtval = (TextBox)Row.FindControl("txtvalue");
                    if (string.IsNullOrEmpty(txtval.Text))
                        txtval.Text = "0";
                    re = School.Teaching.Teaching.Add_Topic_HomeWork(Id, Convert.ToInt32(ddlTopic.SelectedValue), Convert.ToDecimal(txtval.Text));
                }

            }

            if (re == true)
            {

                gvBank_QBind();
                gvBank_QinBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;

        }

        protected void gvBank_Qin_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //txtval

            int Id = Convert.ToInt32(gvBank_Qin.DataKeys[e.RowIndex].Value.ToString());
            TextBox txtval = (TextBox)gvBank_Qin.Rows[e.RowIndex].FindControl("txtval");


            bool re = School.Teaching.Teaching.Updat_Topic_Q(Id, Convert.ToDecimal(txtval.Text));
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvBank_Qin.EditIndex = -1;
                gvBank_QinBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvBank_Qin_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }

        protected void gvBank_Qin_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBank_Qin.PageIndex = e.NewPageIndex;
            gvBank_QinBind();
        }

        protected void gvBank_Qin_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvBank_Qin.EditIndex = -1;
            gvBank_QinBind();
        }

        protected void gvBank_Qin_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvBank_Qin.EditIndex = e.NewEditIndex;
            gvBank_QinBind();
        }

        protected void gvBank_Qin_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvBank_Qin.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Teaching.Teaching.DeleteTopic_Question(Id);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم الحذف بنجاح";
                gvBank_QBind();
                gvBank_QinBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }
    }
}